﻿using NET6.WebAPI.Extensions;
using NET6.WebAPI.Models.Tool;

namespace NET6.WebAPI.Helpers
{
    public class EnumHelper
    {
        /// <summary>
        /// 根据枚举类型获取所有元素信息
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <returns></returns>
        public static IEnumerable<EnumInfo> GetEnumInfos(Type enumType)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException("Type must be an Enum");
            }

            var infos = new List<EnumInfo>();

            foreach (Enum item in Enum.GetValues(enumType))
            {
                var info = new EnumInfo
                {
                    Name = item.ToString(),
                    Value = Convert.ToInt32(item),
                    Describe = item.GetDescription()
                };

                infos.Add(info);
            }

            return infos;
        }
    }
}
